﻿using MCUPackage.DesignEditor;
using Newtonsoft.Json;
using System;
using System.ComponentModel;

namespace SxPackages.STC32G.Models
{
	[Serializable]
	class AllSPIOptions
	{
		/// <summary>
		/// 串口配置结合
		/// </summary>
		[Browsable(false),
		JsonIgnore]
		public SPIOptionBase[] All { get; private set; }

		[Category("SPI配置"),
		DisplayName("通用SPI"),
		TypeConverter(typeof(PropertySorter))]
		public SPIOptions SPI { get; private set; }

		[Category("SPI配置"),
		DisplayName("串口1作为SPI口"),
		Browsable(false),
		TypeConverter(typeof(PropertySorter))]
		public USARTSPIOptions USART1 { get; private set; }

		[Category("SPI配置"),
		DisplayName("串口2作为SPI口"),
		Browsable(false),
		TypeConverter(typeof(PropertySorter))]
		public USARTSPIOptions USART2 { get; private set; }

		[Category("SPI配置"),
		DisplayName("模拟SPI口"),
		TypeConverter(typeof(PropertySorter))]
		public SoftSPIOptions Soft { get; private set; }

		public AllSPIOptions(STC32GQFP64 aParent)
		{
			this.SPI = new SPIOptions(aParent);
			this.USART1 = new USARTSPIOptions(SPITypes.USART1, aParent);
			this.USART2 = new USARTSPIOptions(SPITypes.USART2, aParent);
			this.Soft = new SoftSPIOptions(aParent);
			All = new SPIOptionBase[] { this.SPI, USART1, USART2, Soft };
			this.USART1.Enable = this.USART2.Enable = false;
		}

		public bool HasEnabled()
		{
			foreach (var spi in this.All)
			{
				if (spi.Enable)
				{
					return true;
				}
			}

			return false;
		}
	}
}
